// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.iso;

import gnu.trove.map.hash.THashMap;
import java.util.ArrayList;
import java.util.Map.Entry;
import se.krka.kahlua.j2se.KahluaTableImpl;
import se.krka.kahlua.vm.KahluaTableIterator;
import zombie.core.textures.Texture;
import zombie.inventory.ItemContainer;
import zombie.iso.objects.IsoStove;
import zombie.network.GameServer;
import zombie.util.LocationRNG;
import zombie.util.StringUtils;

public class ContainerOverlays {
    public static final ContainerOverlays instance = new ContainerOverlays();
    private static final ArrayList<ContainerOverlays.ContainerOverlayEntry> tempEntries = new ArrayList();
    private final THashMap<String, ContainerOverlays.ContainerOverlay> overlayMap = new THashMap<>();

    private void parseContainerOverlayMapV0(KahluaTableImpl kahluaTableImpl0) {
        for (Entry entry0 : kahluaTableImpl0.delegate.entrySet()) {
            String string0 = entry0.getKey().toString();
            ContainerOverlays.ContainerOverlay containerOverlay = new ContainerOverlays.ContainerOverlay();
            containerOverlay.name = string0;
            this.overlayMap.put(containerOverlay.name, containerOverlay);
            KahluaTableImpl kahluaTableImpl1 = (KahluaTableImpl)entry0.getValue();

            for (Entry entry1 : kahluaTableImpl1.delegate.entrySet()) {
                String string1 = entry1.getKey().toString();
                KahluaTableImpl kahluaTableImpl2 = (KahluaTableImpl)entry1.getValue();
                String string2 = null;
                if (kahluaTableImpl2.delegate.containsKey(1.0)) {
                    string2 = kahluaTableImpl2.rawget(1.0).toString();
                }

                String string3 = null;
                if (kahluaTableImpl2.delegate.containsKey(2.0)) {
                    string3 = kahluaTableImpl2.rawget(2.0).toString();
                }

                ContainerOverlays.ContainerOverlayEntry containerOverlayEntry = new ContainerOverlays.ContainerOverlayEntry();
                containerOverlayEntry.manyItems = string2;
                containerOverlayEntry.fewItems = string3;
                containerOverlayEntry.room = string1;
                containerOverlay.entries.add(containerOverlayEntry);
            }
        }
    }

    private void parseContainerOverlayMapV1(KahluaTableImpl kahluaTableImpl0) {
        KahluaTableIterator kahluaTableIterator0 = kahluaTableImpl0.iterator();

        while (kahluaTableIterator0.advance()) {
            String string0 = kahluaTableIterator0.getKey().toString();
            if (!"VERSION".equalsIgnoreCase(string0)) {
                ContainerOverlays.ContainerOverlay containerOverlay = new ContainerOverlays.ContainerOverlay();
                containerOverlay.name = string0;
                KahluaTableImpl kahluaTableImpl1 = (KahluaTableImpl)kahluaTableIterator0.getValue();
                KahluaTableIterator kahluaTableIterator1 = kahluaTableImpl1.iterator();

                while (kahluaTableIterator1.advance()) {
                    KahluaTableImpl kahluaTableImpl2 = (KahluaTableImpl)kahluaTableIterator1.getValue();
                    String string1 = kahluaTableImpl2.rawgetStr("name");
                    KahluaTableImpl kahluaTableImpl3 = (KahluaTableImpl)kahluaTableImpl2.rawget("tiles");
                    ContainerOverlays.ContainerOverlayEntry containerOverlayEntry = new ContainerOverlays.ContainerOverlayEntry();
                    containerOverlayEntry.manyItems = (String)kahluaTableImpl3.rawget(1);
                    containerOverlayEntry.fewItems = (String)kahluaTableImpl3.rawget(2);
                    if (StringUtils.isNullOrWhitespace(containerOverlayEntry.manyItems) || "none".equalsIgnoreCase(containerOverlayEntry.manyItems)) {
                        containerOverlayEntry.manyItems = null;
                    }

                    if (StringUtils.isNullOrWhitespace(containerOverlayEntry.fewItems) || "none".equalsIgnoreCase(containerOverlayEntry.fewItems)) {
                        containerOverlayEntry.fewItems = null;
                    }

                    containerOverlayEntry.room = string1;
                    containerOverlay.entries.add(containerOverlayEntry);
                }

                this.overlayMap.put(containerOverlay.name, containerOverlay);
            }
        }
    }

    public void addOverlays(KahluaTableImpl kahluaTableImpl) {
        int _int = kahluaTableImpl.rawgetInt("VERSION");
        if (_int == -1) {
            this.parseContainerOverlayMapV0(kahluaTableImpl);
        } else {
            if (_int != 1) {
                throw new RuntimeException("unknown overlayMap.VERSION " + _int);
            }

            this.parseContainerOverlayMapV1(kahluaTableImpl);
        }
    }

    public boolean hasOverlays(IsoObject obj) {
        return obj != null && obj.sprite != null && obj.sprite.name != null && this.overlayMap.containsKey(obj.sprite.name);
    }

    public void updateContainerOverlaySprite(IsoObject obj) {
        if (obj != null) {
            if (!(obj instanceof IsoStove)) {
                IsoGridSquare isoGridSquare = obj.getSquare();
                if (isoGridSquare != null) {
                    String string0 = null;
                    ItemContainer itemContainer = obj.getContainer();
                    if (obj.sprite != null && obj.sprite.name != null && itemContainer != null && itemContainer.getItems() != null && !itemContainer.isEmpty()) {
                        ContainerOverlays.ContainerOverlay containerOverlay = this.overlayMap.get(obj.sprite.name);
                        if (containerOverlay != null) {
                            String string1 = "other";
                            if (isoGridSquare.getRoom() != null) {
                                string1 = isoGridSquare.getRoom().getName();
                            }

                            ContainerOverlays.ContainerOverlayEntry containerOverlayEntry = containerOverlay.pickRandom(
                                string1, isoGridSquare.x, isoGridSquare.y, isoGridSquare.z
                            );
                            if (containerOverlayEntry == null) {
                                containerOverlayEntry = containerOverlay.pickRandom("other", isoGridSquare.x, isoGridSquare.y, isoGridSquare.z);
                            }

                            if (containerOverlayEntry != null) {
                                string0 = containerOverlayEntry.manyItems;
                                if (containerOverlayEntry.fewItems != null && itemContainer.getItems().size() < 7) {
                                    string0 = containerOverlayEntry.fewItems;
                                }
                            }
                        }
                    }

                    if (!StringUtils.isNullOrWhitespace(string0) && !GameServer.bServer && Texture.getSharedTexture(string0) == null) {
                        string0 = null;
                    }

                    obj.setOverlaySprite(string0);
                }
            }
        }
    }

    public void Reset() {
        this.overlayMap.clear();
    }

    private static final class ContainerOverlay {
        public String name;
        public final ArrayList<ContainerOverlays.ContainerOverlayEntry> entries = new ArrayList();

        public void getEntries(String string, ArrayList<ContainerOverlays.ContainerOverlayEntry> arrayList) {
            arrayList.clear();

            for (int _int = 0; _int < this.entries.size(); _int++) {
                ContainerOverlays.ContainerOverlayEntry containerOverlayEntry = (ContainerOverlays.ContainerOverlayEntry)this.entries.get(_int);
                if (containerOverlayEntry.room.equalsIgnoreCase(string)) {
                    arrayList.add(containerOverlayEntry);
                }
            }
        }

        public ContainerOverlays.ContainerOverlayEntry pickRandom(String string, int int1, int int2, int int3) {
            this.getEntries(string, ContainerOverlays.tempEntries);
            if (ContainerOverlays.tempEntries.isEmpty()) {
                return null;
            } else {
                int int0 = LocationRNG.instance.nextInt(ContainerOverlays.tempEntries.size(), int1, int2, int3);
                return (ContainerOverlays.ContainerOverlayEntry)ContainerOverlays.tempEntries.get(int0);
            }
        }
    }

    private static final class ContainerOverlayEntry {
        public String room;
        public String manyItems;
        public String fewItems;
    }
}
